using System;
using System.IO;
using System.Windows.Forms;
using BizTalk.BuildGenerator.MetaData;

namespace BizTalk.BuildGenerator.UI
{
    public partial class OpenWizardForm : Form
    {
        private readonly Form _host;

        public OpenWizardForm()
        {
            InitializeComponent();
        }

        public OpenWizardForm(Form host)
        {
            _host = host;
            InitializeComponent();
        }

        private void BrowseFileButtonClick(object sender, EventArgs e)
        {
            var dr = openFileDialog.ShowDialog();
            if (dr == DialogResult.OK)
            {
                PathTextBox.Text = openFileDialog.FileName;
            }
        }

        private void OpenWizardButtonClick(object sender, EventArgs e)
        {
            if (!File.Exists(PathTextBox.Text))
            {
                MessageBox.Show(@"The file does not exist");
                return;
            }

            GenerationArgs args;
            try
            {
                args = ArgsHelper.Open(PathTextBox.Text);
            }
            catch
            {
                MessageBox.Show(
                    @"There was an error reading the saved file, please check its format as it seems to be invalid");
                return;
            }
            var wizard = new WizardForm(args);
            if (_host != null)
                wizard.MdiParent = _host;

            wizard.Show();
            Close();
        }
    }
}